
import { globalUtils } from '../common/js/global-utils/global-utils-all';
import { uiUtils } from '../common/js/ui-utils/ui-utils-all';
import Pure from 'pure-frontend';
import { CurrentRunningApp } from '../common/js/current-running-app';
import $ from 'jquery';

/* SOURCE-CODE-START */

"use strict";


Pure.app.viewMain('menu-management', function (scope, view) {
  var jqTree = view.$ui('menuTree');
  var jqSwapTemplateBtn = view.$ui('swapTemplateBtn');
  var jqNewMenuBtn = view.$ui('newMenuBtn');
  var dataModel = view.getDataModel();
  var lastActivateNodeInfo;

  // 初始表格
  uiUtils.dataGrid.init(view.$ui('menuTable'), {
    sidePagination: 'client',
    columns: [
      {
        field: 'id'
      },
      {
        field: 'menuCode'
      },
      {
        field: 'menuName'
      },
      {
        field: 'parentName'
      },
      {
        field: 'menuIcon'
      },
      {
        field: 'menuPath'
      },
      {
        field: 'menuOrder'
      },
      {
        field: 'showFlagDesc'
      },
      {
        field: 'remark'
      },
      {
        field: 'operate',
        'class': 'x-nowrap-items',
        formatter: formatOprationColumn,
        events: {
          'click .delete': clickDeleteOperationFn,
          'click .edit': clickEditOperationFn,
          'click .assignFunction': clickAssignFunctionOperationFn
        }
      }
    ]
  });

  function formatOprationColumn(value, row, index) {
    return [
      '<a class="edit" href="javascript:void(0)">',
      '<span>${t("ui.system.common.operation.edit")}</span>',
      '</a>',
      '&nbsp;',
      '<a class="delete" href="javascript:void(0)">',
      '<span>${t("ui.system.common.operation.delete")}</span>',
      '</a>',
      '&nbsp;',
      '<a class="assignFunction" href="javascript:void(0)">',
      '<span>${t("ui.menu.common.operation.assignFunction")}</span>',
      '</a>'
    ].join('');
  }

  // 加载菜单数据到树形控件
  view.callService('api/menu/menuTemplate/getCurrentMenuTemplate')
    .waitResult(function (result) {
      if (uiUtils.common.checkWebResultSuccess(result)) {
        var templateData = result.data;

        scope.loadTreeMenuStructure(view, templateData);
      }
    });

  // 菜单结构加载完成后选择菜单模板节点
  uiUtils.tree.bindEventOnInit(jqTree, uiUtils.event.TREE_READY, function () {
    var nodes = uiUtils.tree.getSelectedNodes(jqTree);

    if (nodes.length > 0) {
      jqTree.trigger(uiUtils.event.TREE_NODE_ACTIVATE, {
        node: nodes[0]
      });
    }
  });

  // 点击树形控件节点的事件
  uiUtils.tree.bindEventOnInit(jqTree, 'activate_node.jstree', function (e, data) {
    var nodeInfo = data.node.original.nodeInfo;
    var nodeType = nodeInfo.type;
    var childMenus = globalUtils.emptyArrayIfNullOrUndefined(nodeInfo.childMenus);

    if (nodeType === 'template' || nodeType === 'menu') {
      jqNewMenuBtn.removeAttr('disabled');
    } else {
      jqNewMenuBtn.attr('disabled', 'disabled')
    }

    var childMenuData = $.map(childMenus, function (row) {
      return {
        'id': row.id,
        'templateId': row.templateId,
        'menuCode': row.menuCode,
        'menuName': row.menuName,
        'parentId': row.parentId,
        'parentCode': row.parentCode,
        'parentName': row.parentName,
        'menuIcon': row.menuIcon,
        'menuPath': row.menuPath,
        'menuOrder': row.menuOrder,
        'showFlag': row.showFlag,
        'showFlagDesc': row.showFlagDesc,
        'remark': row.remark,
        'versionNumber': row.versionNumber,
      }
    });

    dataModel.setData('childMenuData', childMenuData);

    // 记录最后点击的节点
    lastActivateNodeInfo = nodeInfo;
  });

  // 点击"切换菜单模板"的事件
  jqSwapTemplateBtn.on('click', function () {
    // TODO
    alert('${t("ui.system.common.msg.functionIsNotImplemented")}');
  });

  // 点击"新建菜单"的事件
  jqNewMenuBtn.on('click', function () {
    var templateId = null;
    var parentMenu = null;

    if (lastActivateNodeInfo.type === 'menu') {
      parentMenu = lastActivateNodeInfo.data;
      templateId = parentMenu.templateId
    } else if (lastActivateNodeInfo.type === 'template') {
      var templateData = lastActivateNodeInfo.data;
      templateId = templateData.id;
    }

    // 显示新建菜单的对话框
    scope.showNewMenuDialog(view, templateId, parentMenu);
  });


  /**
   * 处理点击"删除"的事件
   * @param {*} e 
   * @param {*} value 
   * @param {*} row 
   * @param {*} index 
   */
  function clickDeleteOperationFn(e, value, row, index) {
    var data = {
      id: row.id
    };

    uiUtils.dialog.showConfirmBox('${t("ui.system.common.msg.confirmDeleteReocrd")}', function () {
      // 调用删除记录的接口
      view.callService('api/menu/menu/deleteMenuById', data)
        .waitResult(function (result, textStatus, jqXHR) {
          if (result.success) {
            scope.refreshTreeMenuStructure();
            Pure.app.showMessage('${t("ui.system.common.msg.deleteRecordSuccess")}');
          } else {
            Pure.app.showError(result.message);
          }
        });
    });
  }

  /**
   * 处理点击"分配编辑"的事件
   * @param {*} e 
   * @param {*} value 
   * @param {*} row 
   * @param {*} index 
   */
  function clickEditOperationFn(e, value, row, index) {
    // 显示编辑的对话框
    scope.showEditMenuDialog(view, row);
  }

  /**
   * 处理点击"分配功能"的事件
   * @param {*} e 
   * @param {*} value 
   * @param {*} row 
   * @param {*} index 
   */
  function clickAssignFunctionOperationFn(e, value, row, index) {
    // 显示分配功能的对话框
    scope.showAssignFunctionDialog(view, row.id);
  }

});

/* SOURCE-CODE-END */
